<?php
    //没有登录,不能进入到购物车页面
    if(empty($_SESSION['home'])){
        redirect("请登录",1,ROOT_URL."login.php");
        die;
    }
    //获取该用户id
    $user_id = $_SESSION['home']['id'];

    //在购物车页面,如果在该页面增加了或减少的商品的数量也要把该变化带入数据库
    if(!empty($_GET['number']) && !empty($_GET['goods_id'])){
        $number = $_GET['number'];
        $goods_id = $_GET['goods_id'];
        //先查询该用户的购物车中该商品的数量
        $sql = "select goods_number from ".PRE."cart where goods_id = '$goods_id' and user_id = '$user_id'";
        $res = getRow($link, $sql);
        $goods_number = $res['goods_number'];

        if(($number == "minus") && ($goods_number > 1)){
            $sql = "update ".PRE."cart set goods_number = goods_number - 1 where goods_id = '$goods_id' and user_id = '$user_id'";
        }elseif($number == "plus"){
            $sql = "update ".PRE."cart set goods_number = goods_number + 1 where goods_id = '$goods_id' and user_id = '$user_id'";
        }


        execute($link,$sql);
    }

    //查询该用户的购物车记录
    $sql = "
        select
        c.goods_id,c.goods_name,c.goods_price,c.goods_number,i.iname
        from
        ".PRE."image as i,".PRE."cart as c
        where
        i.goods_id = c.goods_id and user_id = '$user_id' and i.cover = 1

    ";

    $cart_info = query($link, $sql);

//    foreach($cart_info as $key => $value){
//        $arr[$key] = $value['goods_id'];
//    }

    //查询用户的收货地址
    //现获取用户id
    $user_id= $_SESSION['home']['id'];
    $sql = "select id,address,default_a from ". PRE . "user_address where user_id = '$user_id'";

    $address_list = query($link, $sql);

?>
<form action="<?php echo ROOT_URL?>pay.php" method="post">
    <div class="container mt50">
        <div class="row mt20">
            <h3><b>确认收货地址</b></h3>
            <?php if(empty($address_list)):?>
                <a href="./add_address.php"><span class="btn btn-primary">新建地址</span></a>
            <?php else:?>
                <?php foreach($address_list as $value):?>
                    <?php if($value['default_a'] == 1):?>
                        <div class="radio col-md-12">
                            <label>
                                <input type="radio" name="adress" value="<?php echo $value['id']?>" checked><?php echo $value['address']?>
                            </label>
                        </div>
                        <?php continue;?>
                    <?php endif;?>
                    <div class="radio col-md-12">
                        <label>
                            <input type="radio" name="adress" value="<?php echo $value['id']?>"><?php echo $value['address']?>&nbsp;&nbsp;&nbsp;&nbsp;<a href="./com/buydo.php?act=set_default_address&id=<?php echo $value['id']?>">设为默认地址</a>
                        </label>
                    </div>
                <?php endforeach;?>
                <br>
                <a href="./add_address.php"><span class="btn btn-primary">新增地址</span></a>
            <?php endif;?>


        </div>

    </div>
    <div class="container mt50">

        <div class="row mt10">
            <h2><b>购物车</b></h2>
        </div>
        <div class="row col-md-12 mt30">
            <?php if(empty($cart_info)):?>
                <h2 class="text-center"><b><a href="<?php echo ROOT_URL?>index.php">您的购物车是空的哦,快去添些东西吧</a></b></h2>
            <?php else:?>

                    <table class="row col-md-12 text-center table" cellpadding="0" cellspacing="0">
                        <tr>
                            <th class="text-center">商品图片</th>
                            <th class="text-center">商品名称</th>
                            <th class="text-center">商品价格</th>
                            <th class="text-center">商品数量</th>
                            <th class="text-center">小计</th>
                            <th class="text-center">操作</th>
                        </tr>
                        <?php if(!empty($cart_info)):?>
                            <?php foreach($cart_info as $key =>$value):?>
                                <input type="hidden" name="<?php echo $key?>" value="<?php echo $value['goods_id']?>">
                                <input type="hidden" name="price<?php echo $value['goods_id']?>" value="<?php echo $value['goods_price']?>">
                                <tr>
                                    <!--商品图片-->
                                    <td class="col-md-2">
                                        <a href="<?php echo ROOT_URL?>goods_details.php?id=<?php echo $value['goods_id']?>">
                                            <img src="<?php echo get_img_url(ROOT_URL . 'upload/',$value['iname'],50)?>">
                                        </a>
                                    </td>
                                    <!--商品名称-->
                                    <td class="col-md-2">
                                        <a href="<?php echo ROOT_URL?>goods_details.php?id=<?php echo $value['goods_id']?>">
                                            <?php echo $value['goods_name']?>
                                        </a>
                                    </td>
                                    <!--商品价格-->
                                    <td class="col-md-2"><h4 id="price<?php echo $value['goods_id'];?>"><?php echo $value['goods_price']?></h4></td>
                                    <!--商品数量-->
                                    <td class="col-md-2">
                                        <span class="price">
                                            <a href="<?php echo ROOT_URL?>cart.php?number=minus&goods_id=<?php echo $value['goods_id'];?>"><div class="col-md-3  btn btn-default" onclick="minus(<?php echo $value['goods_id'];?>)">▬</div></a>
                                            <div class="col-md-6">
                                                <input type="number" name="qty<?php echo $value['goods_id'];?>" value="<?php echo $value['goods_number'];?>" id="number<?php echo $value['goods_id'];?>" class="form-control text-center" onchange="change_num(<?php echo $value['goods_id'];?>)">
                                            </div>
                                            <a href="<?php echo ROOT_URL?>cart.php?number=plus&goods_id=<?php echo $value['goods_id'];?>"><div class="col-md-3 btn btn-default" onclick="plus(<?php echo $value['goods_id'];?>)">✚</div></a>
                                        </span>
                                    </td>
                                    <!--商品小计-->
                                    <td class="col-md-2"><h4 id="total<?php echo $value['goods_id'];?>"><?php echo $value['goods_price'] * $value['goods_number']?></h4></td>
                                    <td class="col-md-2"><a href="./com/buydo.php?act=delete_cart&goods_id=<?php echo $value['goods_id']?>">删除</a></td>
                                </tr>
                            <?php endforeach;?>
                            <tr>
                                <td colspan="7" class="text-right">
                                    <button class="btn btn-danger" type="submit">提交订单</button>
                                </td>
                            </tr>
                        <?php endif;?>
                    </table>


            <?php endif;?>
        </div>
    </div>
</form>
